package com.xz.courseschedule.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xz.courseschedule.common.vo.BaseVo;
import com.xz.courseschedule.entity.Classes;
import com.xz.courseschedule.mapper.ClassesMapper;
import com.xz.courseschedule.service.ClassesService;
import com.xz.courseschedule.utils.Result;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: zys
 * @CreateTime: 2025-05-23
 */

@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements ClassesService{
    @Override
    public Result getList() {
        List<Classes> list = super.list();
        return Result.success("成功返回班级表",list);
    }

    @Override
    public Result getClassNo() {
        LambdaQueryWrapper<Classes> wrapper = new LambdaQueryWrapper<Classes>()
                .select(Classes::getClassNo);
        List<Classes> list = super.list(wrapper);
        Set<String> set =  list.stream().map(Classes::getClassNo).collect(Collectors.toSet());
        return Result.success("成功返回班级集合",set);
    }

    @Override
    public Result getById(Long id) {
        if (id == null) {
            return Result.error(400, "班级ID不能为空");
        }
        Classes classes = super.getById(id);
        if (classes == null) {
            return Result.error(404, "班级不存在");
        }
        return Result.success("成功获取班级信息", classes);
    }

    @Override
    public Result add(Classes classes) {
        if (classes == null) {
            return Result.error(400, "班级信息不能为空");
        }
        // 检查班级编号是否已存在
        if (classes.getClassNo() != null) {
            LambdaQueryWrapper<Classes> wrapper = new LambdaQueryWrapper<Classes>()
                    .eq(Classes::getClassNo, classes.getClassNo());
            Classes existingClasses = super.getOne(wrapper);
            if (existingClasses != null) {
                return Result.error(400, "班级编号已存在");
            }
        }
        boolean result = super.save(classes);
        if (result) {
            return Result.success("班级添加成功", classes);
        } else {
            return Result.error(500, "班级添加失败");
        }
    }

    @Override
    public Result update(Classes classes) {
        if (classes == null || classes.getId() == null) {
            return Result.error(400, "班级信息或ID不能为空");
        }
        // 检查班级是否存在
        Classes existingClasses = super.getById(classes.getId());
        if (existingClasses == null) {
            return Result.error(404, "班级不存在");
        }
        // 检查班级编号是否与其他班级重复
        if (classes.getClassNo() != null) {
            LambdaQueryWrapper<Classes> checkWrapper = new LambdaQueryWrapper<Classes>()
                    .eq(Classes::getClassNo, classes.getClassNo())
                    .ne(Classes::getId, classes.getId());
            Classes duplicateClasses = super.getOne(checkWrapper);
            if (duplicateClasses != null) {
                return Result.error(400, "班级编号已存在");
            }
        }
        boolean result = super.updateById(classes);
        if (result) {
            return Result.success("班级更新成功", classes);
        } else {
            return Result.error(500, "班级更新失败");
        }
    }

    @Override
    public Result delete(Long id) {
        if (id == null) {
            return Result.error(400, "班级ID不能为空");
        }
        // 物理删除
        boolean result = super.removeById(id);
        if (result) {
            return Result.success("班级删除成功");
        } else {
            return Result.error(500, "班级删除失败");
        }
    }

    @Override
    public Result deleteBatch(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Result.error(400, "班级ID列表不能为空");
        }
        // 物理删除
        boolean result = super.removeByIds(Arrays.asList(ids));
        if (result) {
            return Result.success("批量删除班级成功");
        } else {
            return Result.error(500, "批量删除班级失败");
        }
    }
}
